#include "DoubanConfigWidget.h"
#include "utils/InfoEncrypter.h"
#include <QApplication>
#include <QSettings>
namespace Douban
{
	using namespace TWCore;
	DoubanConfigWidget::DoubanConfigWidget() :
			IConfigWidget()
	{
		ui.setupUi(this);
		readSetting();
	}

	DoubanConfigWidget::~DoubanConfigWidget(){}

	void DoubanConfigWidget::readSetting()
	{
		QSettings setting(qApp->applicationDirPath() + "/setting.ini",QSettings::IniFormat);
		setting.beginGroup("Douban");
		ui.miniBlogUpdateCB->setValue(setting.value("MiniBlogUpdateTime",20).toInt());
		ui.miniBlogQuantityCB->setValue(setting.value("MaxMiniBlogAmount",30).toInt());
		ui.mailUpdateCB->setValue(setting.value("MailUpdateTime",30).toInt());

		ui.notifyGB->setChecked(setting.value("EnableNotifyChecker",false).toBool());
		ui.notifyIntervalSB->setValue(setting.value("CheckNotifyInterval",15).toInt());

		QString dataPath = qApp->applicationDirPath() + "/data/doubane.dat";
		Utils::InfoEncrypter encrypter("011c7d17abb086cd",dataPath);
		email = encrypter.value("DoubanEmailAddress");
		password = encrypter.value("DoubanPassword");
		ui.emailText->setText(email);
		ui.passwordText->setText(password);

		setting.endGroup();
		setting.beginGroup( "DoubanFM" );
		ui.groupBox->setChecked( setting.value( "EnableFM", true ).toBool() );
		ui.alwaysOnTopCB->setChecked( setting.value( "AlwaysOnTop", false ).toBool() );
		ui.showBorederCB->setChecked( setting.value( "ShowBorder", false ).toBool() );
		ui.showInfoCB->setChecked( setting.value( "ShowInfo", true ).toBool() );
		ui.useTrayIcoCB->setChecked( setting.value( "UseTrayIco", true ).toBool() );

		ui.favEdit->setText( setting.value( "FavShortCut", "" ).toString() );
		ui.delEdit->setText( setting.value( "DeleteShortCut", "" ).toString() );
		ui.dislikeEdit->setText( setting.value( "DisLikeShortCut", "" ).toString() );
		ui.nextEdit->setText( setting.value( "NextShortCut", "" ).toString() );
		ui.stopEdit->setText( setting.value( "StopShortCut", "" ).toString() );
		ui.showEdit->setText( setting.value( "ShowShortCut", "" ).toString() );
	}

	void DoubanConfigWidget::saveSetting()
	{
		QSettings setting(qApp->applicationDirPath() + "/setting.ini",QSettings::IniFormat);
		setting.beginGroup("Douban");
		setting.setValue("MiniBlogUpdateTime",ui.miniBlogUpdateCB->value());
		setting.setValue("MaxMiniBlogAmount",ui.miniBlogQuantityCB->value());
		setting.setValue("MailUpdateTime",ui.mailUpdateCB->value());

		setting.setValue("EnableNotifyChecker",ui.notifyGB->isChecked());
		setting.setValue("CheckNotifyInterval",ui.notifyIntervalSB->value());

		if(!ui.notifyGB->isChecked())
		{
			ui.emailText->clear();
			ui.passwordText->clear();
		}
		QString e = ui.emailText->text();
		QString p = ui.passwordText->text();
		if(email != e || password != p)
		{
			QString dataPath = qApp->applicationDirPath() + "/data/doubane.dat";
			Utils::InfoEncrypter encrypter("011c7d17abb086cd",dataPath);
			encrypter.addInfo("DoubanEmailAddress",e);
			encrypter.addInfo("DoubanPassword",p);
			encrypter.encrypt();
		}

		setting.endGroup();
		setting.beginGroup( "DoubanFM" );
		setting.setValue("EnableFM",ui.groupBox->isChecked());
		setting.setValue("AlwaysOnTop",ui.alwaysOnTopCB->isChecked());
		setting.setValue("ShowBorder",ui.showBorederCB->isChecked());
		setting.setValue("ShowInfo",ui.showInfoCB->isChecked());
		setting.setValue("UseTrayIco",ui.useTrayIcoCB->isChecked());

		setting.setValue("FavShortCut",ui.favEdit->text());
		setting.setValue("DeleteShortCut",ui.delEdit->text());
		setting.setValue("DisLikeShortCut",ui.dislikeEdit->text());
		setting.setValue("NextShortCut",ui.nextEdit->text());
		setting.setValue("StopShortCut",ui.stopEdit->text());
		setting.setValue("ShowShortCut",ui.showEdit->text());
	}
}
